package com.xujianlun.cpms.controller.shop;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xujianlun.cpms.common.Response;

import com.xujianlun.cpms.service.shop.IOrderGoodsService;
import com.xujianlun.cpms.entity.shop.OrderGoods;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 订单物品关联表 前端控制器
 * </p>
 *
 * @author xujianlun
 * @since 2022-12-15
 */
@RestController
@RequestMapping("/order-goods")
public class OrderGoodsController {

    @Resource
    private IOrderGoodsService orderGoodsService;

    // 新增或者更新
    @PostMapping
    public Response save(@RequestBody OrderGoods orderGoods) {
        orderGoodsService.saveOrUpdate(orderGoods);
        return Response.success();
    }

    @DeleteMapping("/{id}")
    public Response delete(@PathVariable Integer id) {
        orderGoodsService.removeById(id);
        return Response.success();
    }

    @PostMapping("/del/batch")
    public Response deleteBatch(@RequestBody List<Integer> ids) {
        orderGoodsService.removeByIds(ids);
        return Response.success();
    }

    @GetMapping
    public Response findAll() {
        return Response.success(orderGoodsService.list());
    }

    @GetMapping("/{id}")
    public Response findOne(@PathVariable Integer id) {
        return Response.success(orderGoodsService.getById(id));
    }

    @GetMapping("/page")
    public Response findPage(@RequestParam Integer pageNum,
                             @RequestParam Integer pageSize) {
        QueryWrapper<OrderGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Response.success(orderGoodsService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

